package robot;

import java.util.Date;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Event;
import com.google.wave.api.EventType;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.Wavelet;

/**
 * Google Wave Status Robot
 * 
 * @author Monika Adamczyk
 */
@SuppressWarnings("serial")
public class StatusRobotServlet extends AbstractRobotServlet
{
	private static final String ROBOT_ID = "statusrobot@appspot.com";

	private static final String WELCOME = "Welcome to Status Robot, " + new Date();

	private static final Logger log = Logger.getLogger(StatusRobotServlet.class
			.getName());

	private StatusController statusController = null;
	
	@Override
	public void processEvents(RobotMessageBundle robotMessageBundle)
	{
		Wavelet wavelet = robotMessageBundle.getWavelet();
		Blip rootBlip = wavelet.getRootBlip();
		
		if (robotMessageBundle.wasSelfAdded())
		{
			wavelet.getRootBlip().getDocument().setAuthor(ROBOT_ID);
			wavelet.setTitle(WELCOME);
			statusController = new StatusController();
			StatusFormController formController = new StatusFormController(wavelet.appendBlip());
			formController.renderForm();

//			OperationFormController operationController = new OperationFormController(wavelet.appendBlip());
//			operationController.renderForm();
			
		}

		for (Event event: robotMessageBundle.getEvents())
		{
			if (event.getType().equals(EventType.FORM_BUTTON_CLICKED))
			{
//				if (event.getButtonName().equals(BaseFormController.ADD_STATUS_BUTTON_ID))
//				{
//					Blip formBlip = wavelet.appendBlip();
//					StatusFormController statusController = new StatusFormController(formBlip);
//					statusController.renderForm();
//				}
//				else if (event.getButtonName().equals(BaseFormController.GET_STATUS_BUTTON_ID))
//				{
//					renderStatusList(wavelet.getRootBlip(), wavelet.getWaveId());
//				}
				
				if (event.getButtonName().equals(BaseFormController.SUBMIT_BUTTON_ID))
				{
					// retrieve state of the form from the blip
					StatusFormController statusFormController = new StatusFormController(event.getBlip());
					if (statusFormController.isSubmitButtonClicked(event))
					{
	
						String name = statusFormController.getNameInput();
						String status = statusFormController.getStatusInput();
						log.info("Name input " + name);
						log.info("Status input " + status);
						
						if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(status))
						{
							statusFormController.renderForm();
							statusController.updateStatus(wavelet.getWaveId(), name, status);
							statusController.renderStatusList(wavelet.getRootBlip(), wavelet.getWaveId());
//							wavelet.getRootBlip().getDocument().append("\n" + name + " : " + status);
//							log.info("Status updated, sending thankyou blip");
//							blip.getDocument().append("\nThank you " 
//									+ name
//									+ ",  your status has been updated to "
//									+ status);
						}
						else
						{
							// ask for input again
							log.info("Missing imput, asking for it again");
							Blip blip = wavelet.appendBlip();
							blip.getDocument().append("\nPlease enter name and status\n");
						}
					}
				}
			}
		}
		

	}

}
